package com.looookme.shaozhuo.broadcast_service;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * Created by Shaozhuo on 2015/9/9.
 */
public class BroadCastDemo extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    private void initView() {
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ll.setOrientation(LinearLayout.VERTICAL);
        setContentView(ll);

        //需要在AndroidManifest.xml中注册Receiver
        /*
        <receiver
            android:name="com.looookme.shaozhuo.broadcast_service.MyReceiver"
            android:enabled="true"
            android:exported="true" >
        </receiver>
         */
//        Button sendMsg = new Button(this);
//        sendMsg.setText("Send BroadCase Message");
//        sendMsg.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        ll.addView(sendMsg);
//        sendMsg.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                sendBroadcast(new Intent(BroadCastDemo.this, MyReceiver.class));
//            }
//        });

        //动态注册BroadCastReceiver
        Button regReceiver = new Button(this);
        regReceiver.setText("Reg BroadCastReceiver");
        regReceiver.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        ll.addView(regReceiver);
        regReceiver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(null == receiver){
                    receiver = new MyReceiver();
                    registerReceiver(receiver, new IntentFilter(MyReceiver.ACTION));
                }
            }
        });

        //动态注销BroadCastReceiver
        Button unRegReceiver = new Button(this);
        unRegReceiver.setText("UnReg BroadCastReceiver");
        unRegReceiver.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        ll.addView(unRegReceiver);
        unRegReceiver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(null != receiver){
                    unregisterReceiver(receiver);
                    receiver = null;
                }
            }
        });

        Button sendMsg = new Button(this);
        sendMsg.setText("Send BroadCase Message");
        sendMsg.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        ll.addView(sendMsg);
        sendMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendBroadcast(new Intent(MyReceiver.ACTION));
            }
        });
    }
    private MyReceiver receiver = null;
}
